package com.syg.servlet11_Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

/**
 * filter
 */
public class _01_FilterDemo implements Filter{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public _01_FilterDemo() {
        logger.info("A1,filter无参构造执行了(第一个filter)");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("A2,filter初始化方法执行了(第一个filter)");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        logger.info("A3,filter放行之前(第一个filter)");
        filterChain.doFilter(servletRequest,servletResponse);//让目标资源执行
        logger.info("A4,filter放行之后(第一个filter)");
    }

    @Override
    public void destroy() {
        logger.info("A5,Filter销毁了(第一个filter)");
    }
}
